<?php
/**
 * @Name: Users.php
 * @Author: yashuai<1762910894@qq.com>
 */

namespace app\backstage\controller\rbac;

use think\Request;
use think\facade\Db;
use app\common\requests\StatusRequest;
use app\common\services\rbac\NodeServices;
use app\common\services\rbac\UsersServices;
use app\backstage\controller\BaseController;
use app\common\requests\rbac\UserSaveRequest;
use app\common\requests\rbac\UserCustomRequest;

class Users extends BaseController
{

    public function __construct(UsersServices $service)
    {
        $this->service = $service;
    }

    /**
     * @Desc 用户列表
     * @param Request $request
     * @return \think\response\Json
     * @author yashuai<1762910894@qq.com>
     */
    public function index(Request $request)
    {
        $params = $request->all();
        $result = $this->service->getList($params);
        return _success($result);
    }

    /**
     * @Desc 添加用户
     * @param UserSaveRequest $request
     * @return mixed
     * @author yashuai<1762910894@qq.com>
     */
    public function create(UserSaveRequest $request)
    {
        $params = $request->getValidationData([
            'UserPassword' => 'require|length:6,32|alphaDash'
        ]);
        return Db::transaction(function () use ($params) {
            $this->service->createData($params);
            return _success();
        });
    }

    /**
     * @Desc 编辑用户
     * @param UserSaveRequest $request
     * @return mixed
     * @author yashuai<1762910894@qq.com>
     */
    public function update(UserSaveRequest $request)
    {
        $params = $request->getValidationData([
            'UserID' => 'require|integer'
        ]);
        return Db::transaction(function () use ($params) {
            $this->service->updateData($params);
            return _success();
        });
    }

    /**
     * @Desc 删除用户
     * @param StatusRequest $request
     * @return \think\response\Json
     * @author yashuai<1762910894@qq.com>
     */
    public function status(StatusRequest $request)
    {
        $params = $request->getValidationData();
        $result = $this->service->deleteData($params);
        return _success($result);
    }

    /**
     * @Desc 用户信息
     * @param Request $request
     * @return \think\response\Json
     * @author yashuai<1762910894@qq.com>
     */
    public function info(Request $request)
    {
        $result = $this->service->info($request->userId);
        return _success($result);
    }

    /**
     * @Desc 重置密码
     * @param UserCustomRequest $request
     * @return \think\response\Json
     * @author yashuai<1762910894@qq.com>
     */
    public function resetPassword(UserCustomRequest $request)
    {
        $validator = [
            'UserID'       => 'require|integer',
            'UserPassword' => 'require|length:6,32',
        ];
        $params    = $request->getValidationData($validator);
        $this->service->resetPassword($params);
        return _success();
    }

    /**
     * @Desc 重置自己的密码
     * @param UserCustomRequest $request
     * @return \think\response\Json
     * @author yashuai<1762910894@qq.com>
     */
    public function resetMyPassword(UserCustomRequest $request)
    {
        $validator = [
            'OriginalPassword' => 'require',
            'UserPassword'     => 'require|length:6,32',
        ];
        $params    = $request->getValidationData($validator);
        $params['UserID'] = $request->userId;
        $this->service->resetPassword($params, true);
        return _success();
    }

    /**
     * @Desc 获取用户菜单节点
     * @param Request $request
     * @return \think\response\Json
     * @author yashuai<1762910894@qq.com>
     */
    public function menuNode(Request $request)
    {
        /** @var NodeServices $nodeService */
        $nodeService = app()->make(NodeServices::class);
        $result = $nodeService->getUserAdminNodeTree($request->userId);
        return _success($result);
    }


}
